local skynet = require "skynet"
local filename = "slotmgr.lua"
local filelog = require "filelog"
local queue = require "skynet.queue"
local base = require "base"
local redisdao = require "redisdao"
local tabletool = require "tabletool"
local json = require "cjson"
json.encode_sparse_array(true,1,1)

local SlotTool = require "slottool"
require "bjlenum"

------------
--枚举类

--扑克牌组数
local CardPairNum = 8           --8副牌，每副52张，共计416张

--扑克牌定义
--百位表示花色：1方块 2梅花 3红桃 4黑桃;末2位表示牌值1-A 11-J 12-Q 13—K 2~10-2~10
local CardData = {
    101,102,103,104,105,106,107,108,109,110,111,112,113,
    201,202,203,204,205,206,207,208,209,210,211,212,213,
    301,302,303,304,305,306,307,308,309,310,311,312,313,
    401,402,403,404,405,406,407,408,409,410,411,412,413,
}

--闲家补牌规则(根据前2张牌点数)
--为true表示补牌
local Player_FillCard_Rules = {
    [0] = true,         
    [1] = true,
    [2] = true,
    [3] = true,
    [4] = true,
    [5] = true,
    [6] = false,
    [7] = false,
    [8] = false,
    [9] = false,
}

--庄家补牌规则（根据庄家前2张牌点数和补牌点数）
--为true表示不补牌,-1表示闲未补牌情况
local Banker_FillCard_Rules = {
    [0] = {},
    [1] = {},
    [2] = {},
    [3] = {[8] = true},
    [4] = {[0] = true,[1] = true,[8] = true,[9] = true},
    [5] = {[0] = true,[1] = true,[2] = true,[3] = true,[8] = true,[9] = true},
    [6] = {[-1] = true, [0] = true,[1] = true,[2] = true,[3] = true,[4] = true,[5] = true,[8] = true,[9] = true},
    [7] = false,
    [8] = false,
    [9] = false,
}

--系统输钱上限基值,默认10万
local SystemLoseLimit_Base = 1000000

--系统输钱上限控制系数
local SystemLoseLimit_Factor = 0.08

--开奖类型
local LotteryType = {
    LOTTERY_TYPE_BEST = 1,      --最优开奖
    LOTTERY_TYPE_BAD = 2,       --较差开奖
    LOTTERY_TYPE_NORMAL = 3,    --正常开奖
}

--图标类型
local Picture_type = {
    PICTURE_TYPE_NORMAL = 1,    --普通图标
}

--图标subid（不计类型）
local Picture_subid = {
    101,    --庄
    102,    --闲
    103,    --平
    104,    --庄对
    105,    --闲对
    106,    --庄天王
    107,    --闲天王
}

--图标id
local Picture_id = {
    1101,    --庄
    1102,    --闲
    1103,    --平
    1104,    --庄对
    1105,    --闲对
    1106,    --庄天王
    1107,    --闲天王
}

--特殊奖励定义
local SpecialPrize = {

}

--额外奖励类型列表（所有奖励）
--bjlenum.lua中有对应定义
local Extra_prize_type = {

}

----------------------
--数据表，可以改变

--倍率列表
--key为：图标id = 图标类型 + 图标subid
--[1101] = 0 表示：庄的赔率为2倍
--
local Odds_list = {
    [1101] = 2,     --庄
    [1102] = 2,     --闲
    [1103] = 9,     --平
    [1104] = 12,    --庄对
    [1105] = 12,    --闲对
    [1106] = 3,     --庄天王
    [1107] = 3,     --闲天王
}

--图标权重列表
local Weight_list = {
    --普通开奖权值
    [1] = {
        [1101] = 4500,  --庄
        [1102] = 4500,  --闲
        [1103] = 500,   --平
    },
    --庄家特殊牌权值
    [2] = {
        [1104] = 100,   --庄对
        [1106] = 300,   --庄天王
        [1] = 10,       --天王对
        [0] = 9600,     --非特殊
    },
    --闲家特殊牌权值
    [3] = {
        [1105] = 100,   --闲对
        [1107] = 300,   --闲天王
        [1] = 10,       --天王对
        [0] = 9600,     --非特殊
    },
}

--系统库存概率表
--S = 库存值
local S_list = {
    [1] = { -- S < -500万
        nohit = 1.00,   --必不中概率
        normal = 0.00,  --正常开奖概率
        hit = 0.00,     --必中概率
    },
    [2] = { -- -500万<=S<-100万
        nohit = 0.80,   --必不中概率
        normal = 0.20,  --正常开奖概率
        hit = 0.00,     --必中概率
    },
    [3] = { -- -100万<=S<-20万
        nohit = 0.60,   --必不中概率
        normal = 0.40,  --正常开奖概率
        hit = 0.00,     --必中概率
    },
    [4] = { -- -20万<=S<-10万
        nohit = 0.30,   --必不中概率
        normal = 0.70,  --正常开奖概率
        hit = 0.00,     --必中概率
    },
    [5] = { -- -10万<=S<200万
        nohit = 0.00,   --必不中概率
        normal = 1.00,  --正常开奖概率
        hit = 0.00,     --必中概率
    },
    [6] = { -- 200万<=S<1000万
        nohit = 0.00,   --必不中概率
        normal = 0.80,  --正常开奖概率
        hit = 0.20,     --必中概率
    },
    [7] = { -- 1000万<=S<5000万
        nohit = 0.00,   --必不中概率
        normal = 0.60,  --正常开奖概率
        hit = 0.40,     --必中概率
    },
    [8] = { -- 5000万<=S<1亿
        nohit = 0.00,   --必不中概率
        normal = 0.50,  --正常开奖概率
        hit = 0.50,     --必中概率
    },
    [9] = { -- S>=1亿
        nohit = 0.00,   --必不中概率
        normal = 0.40,  --正常开奖概率
        hit = 0.60,     --必中概率
    },
}

--特殊奖励权值表
local SPrize_weight_list = {
    
}

--开奖权值表
local Jackpot_weight_list = {
    [0] = {
        [1] = 100,  --中奖励1的权重
        [2] = 0,    --中奖励2的权重
        [3] = 0,    --中奖励3的权重
        [4] = 0,    --中奖励4的权重
        [5] = 0,    --中奖励5的权重
        [6] = 0,    --中奖励6的权重
    },
    [1] = {
        [1] = 80,   --中奖励1的权重
        [2] = 20,   --中奖励2的权重
        [3] = 0,    --中奖励3的权重
        [4] = 0,    --中奖励4的权重
        [5] = 0,    --中奖励5的权重
        [6] = 0,    --中奖励6的权重
    },
    [2] = {
        [1] = 60,   --中奖励1的权重
        [2] = 30,   --中奖励2的权重
        [3] = 10,   --中奖励3的权重
        [4] = 0,    --中奖励4的权重
        [5] = 0,    --中奖励5的权重
        [6] = 0,    --中奖励6的权重
    },
    [3] = {
        [1] = 50,   --中奖励1的权重
        [2] = 30,   --中奖励2的权重
        [3] = 10,   --中奖励3的权重
        [4] = 10,   --中奖励4的权重
        [5] = 0,    --中奖励5的权重
        [6] = 0,    --中奖励6的权重
    },
    [4] = {
        [1] = 40,   --中奖励1的权重
        [2] = 30,   --中奖励2的权重
        [3] = 20,   --中奖励3的权重
        [4] = 10,   --中奖励4的权重
        [5] = 0,    --中奖励5的权重
        [6] = 0,    --中奖励6的权重
    },
    [5] = {
        [1] = 30,   --中奖励1的权重
        [2] = 30,   --中奖励2的权重
        [3] = 20,   --中奖励3的权重
        [4] = 10,   --中奖励4的权重
        [5] = 10,   --中奖励5的权重
        [6] = 0,    --中奖励6的权重
    },
    [6] = {
        [1] = 10,   --中奖励1的权重
        [2] = 20,   --中奖励2的权重
        [3] = 20,   --中奖励3的权重
        [4] = 20,   --中奖励4的权重
        [5] = 20,   --中奖励5的权重
        [6] = 10    --中奖励6的权重
    },
}

--奖池奖励列表
local Jackpot_award_list = {
    [1] = 1,    --获取奖池1%
    [2] = 5,    --获取奖池5%
    [3] = 8,    --获取奖池8%
    [4] = 10,   --获取奖池10%
    [5] = 15,   --获取奖池15%
    [6] = 20,   --获取奖池20%
}


--------------------------
--辅助表

--图标概率表，由权重表生成
local Prob_list = nil

--图标总权值,，由权重表生成
local Total_weight = nil

--预存牌型表
local Profile_Cards_List = {
    --庄胜的牌型
    [1101] = { 
        [0] = { --庄普通牌
            [0]    = { },  --闲普通牌
            [1]    = { },  --闲天王对
            [1105] = { },  --闲对子
            [1107] = { },  --闲天王
        },  
        [1] = { --庄天王对
            [0]    = { },  --闲普通牌
            [1]    = { },  --闲天王对
            [1105] = { },  --闲对子
            [1107] = { },  --闲天王
        },  
        [1104] = { --庄对子
            [0]    = { },  --闲普通牌
            [1]    = { },  --闲天王对
            [1105] = { },  --闲对子
            [1107] = { },  --闲天王
        },
        [1106] = { --庄天王
            [0]    = { },  --闲普通牌
            [1]    = { },  --闲天王对
            [1105] = { },  --闲对子
            [1107] = { },  --闲天王
        },
    },
    --闲胜的牌型
    [1102] = { 
        [0] = { --庄普通牌
            [0]    = { },  --闲普通牌
            [1]    = { },  --闲天王对
            [1105] = { },  --闲对子
            [1107] = { },  --闲天王
        },  
        [1] = { --庄天王对
            [0]    = { },  --闲普通牌
            [1]    = { },  --闲天王对
            [1105] = { },  --闲对子
            [1107] = { },  --闲天王
        },  
        [1104] = { --庄对子
            [0]    = { },  --闲普通牌
            [1]    = { },  --闲天王对
            [1105] = { },  --闲对子
            [1107] = { },  --闲天王
        },
        [1106] = { --庄天王
            [0]    = { },  --闲普通牌
            [1]    = { },  --闲天王对
            [1105] = { },  --闲对子
            [1107] = { },  --闲天王
        },
    },
    --平局的牌型
    [1103] = { 
        [0] = { --庄普通牌
            [0]    = { },  --闲普通牌
            [1]    = { },  --闲天王对
            [1105] = { },  --闲对子
            [1107] = { },  --闲天王
        },  
        [1] = { --庄天王对
            [0]    = { },  --闲普通牌
            [1]    = { },  --闲天王对
            [1105] = { },  --闲对子
            [1107] = { },  --闲天王
        },  
        [1104] = { --庄对子
            [0]    = { },  --闲普通牌
            [1]    = { },  --闲天王对
            [1105] = { },  --闲对子
            [1107] = { },  --闲天王
        },
        [1106] = { --庄天王
            [0]    = { },  --闲普通牌
            [1]    = { },  --闲天王对
            [1105] = { },  --闲对子
            [1107] = { },  --闲天王
        },
    },
}

-------------------------------
-------------------------------
---工具类

--更新图标概率表
function SlotTool.update_picture_prob_list()
    local prob_list = {}

    local total_prob = 0
    local total_weight = SlotTool.calculate_total_weight(Weight_list[1])
    for id,w in pairs(Weight_list[1]) do
        prob_list[id] = w / total_weight
        total_prob = total_prob + prob_list[id]
    end
        
    --总概率应为1
    if math.floor(total_prob * 100) ~= 100 then
        filelog.sys_error("total probability not equal to 1!",total_prob)
    end

    return prob_list,total_weight
end

--
--获取开奖类型
--返回 LotteryType类型
--
function SlotTool.get_lottery_type(gamename)
    --获取库存值
    local inventory = SlotTool.get_static_inventory_from_redis(gamename)

    local item = nil
    local s = inventory
    local a = 10000
    if s < -500 * a then
        item = S_list[1]
    elseif s >= -500 * a and s < -100 * a then
        item = S_list[2]
    elseif s >= -100 * a and s < -20 * a  then
        item = S_list[3]
    elseif s >= -20 * a and s < -10 * a then
        item = S_list[4]
    elseif s >= -10 * a and s < 200 * a then
        item = S_list[5]
    elseif s >= 200 * a and s < 1000 * a then
        item = S_list[6]
    elseif s >= 1000 * a and s < 5000 * a then
        item = S_list[7]
    elseif s >= 5000 * a and s < 10000 * a then
        item = S_list[8]
    elseif s >= 10000 * a then
        item = S_list[9]
    end

     --根据概率获取随机开奖类型
    local r = base.get_random(1, 1000)
    local hit = math.floor(item.hit * 1000)
    local nohit = hit + math.floor(item.nohit * 1000)
    local normal = nohit + math.floor(item.normal * 1000) 
    if r <= hit then
        return LotteryType.LOTTERY_TYPE_BAD
    elseif r <= nohit then
        return LotteryType.LOTTERY_TYPE_BEST
    elseif r <= normal then
        return LotteryType.LOTTERY_TYPE_NORMAL
    end
end

--
--获取系统输赢列表
--
function SlotTool.get_system_win_lose_list(total_bet_list, total_bet, is_robot_banker)
    local sys_win_list = {}
    local sys_lose_list = {}

    if is_robot_banker then
        for id,_ in pairs(Weight_list[1]) do
            local bet_id = id
            local odds = Odds_list[id]
            if odds > 0 and total_bet_list[bet_id] then
                local rtm = odds * total_bet_list[bet_id]   --返钱数
                if bet_id == EBJLBetBtnId.BET_BTN_ID_TIE then
                    local bet_id_banker = EBJLBetBtnId.BET_BTN_ID_BANKER
                    local bet_id_player = EBJLBetBtnId.BET_BTN_ID_PLAYER
                    rtm = rtm + total_bet_list[bet_id_banker] + total_bet_list[bet_id_player] 
                end
                if rtm  < total_bet then
                    table.insert(sys_win_list,id)
                elseif rtm > total_bet then
                    table.insert(sys_lose_list,id)
                end
            end
        end
    else
        for id,_ in pairs(Weight_list[1]) do
            local bet_id = id
            local odds = Odds_list[id]
            if odds > 0 and total_bet_list[bet_id] then
                local rtm = odds * total_bet_list[bet_id]   --返钱数
                if bet_id == EBJLBetBtnId.BET_BTN_ID_TIE then
                    local bet_id_banker = EBJLBetBtnId.BET_BTN_ID_BANKER
                    local bet_id_player = EBJLBetBtnId.BET_BTN_ID_PLAYER
                    rtm = rtm + total_bet_list[bet_id_banker] + total_bet_list[bet_id_player] 
                end
                if rtm  > total_bet then
                    table.insert(sys_win_list,id)
                elseif rtm < total_bet then
                    table.insert(sys_lose_list,id)
                end
            end
        end
    end
    
    return sys_win_list,sys_lose_list
end

--
--从预存牌堆获取牌
--
function SlotTool.real_getcards_from_profile_cards(result_id, ext_prize_list, player_result, banker_result)
    --1/2概率直接从牌堆获取
    local is_cards_stack = base.get_random(0,1)
    if is_cards_stack == 1 then
        local card_list = Profile_Cards_List[result_id]
        if card_list and card_list[banker_result] 
        and card_list[banker_result][player_result] then
            local target_list = card_list[banker_result][player_result]
            if #target_list >= 10 then
                local idx = base.get_random(1,#target_list)
                local result = target_list[idx]
                local is_fillcard = false
                local banker_points = 0
                local player_points = 0
                local banker_cards = {}
                local player_cards = {}

                for _,v in pairs(result.banker_cards) do
                    local ct = base.get_random(1,4)
                    local cv = v % 100
                    local card = ct * 100 + cv
                    if cv < 10 then
                        banker_points = (banker_points + cv) % 10
                    end
                    table.insert(banker_cards,card)
                end
                for _,v in pairs(result.player_cards) do
                    local ct = base.get_random(1,4)
                    local cv = v % 100
                    local card = ct * 100 + cv
                    if cv < 10 then
                        player_points = (player_points + cv) % 10
                    end
                    table.insert(player_cards,card)
                end
                if #banker_cards > 2 or #player_cards > 2 then
                    is_fillcard = true
                end
                
                local result_list = {
                    banker_cards = banker_cards,
                    player_cards = player_cards,
                    banker_points = banker_points,
                    player_points = player_points,
                    is_fillcard = is_fillcard,
                    prize_id = result_id,
                    ext_prize_list = ext_prize_list,
                }

                return result_list
            end
        end
    end

    return false
end

--
--从预存牌堆中获取玩家牌
--不再生成特殊牌
--
function SlotTool.get_cards_from_profile_cards(result_id, ext_prize_list, player_result, banker_result)
    --尝试从预存牌堆获取
    local is_profile_cards = SlotTool.real_getcards_from_profile_cards(result_id,ext_prize_list,player_result,banker_result)
    if is_profile_cards then
        return is_profile_cards
    end

    --根据结果随机点数
    local player_points = nil
    local banker_points = nil
    local player_duizi = EBJLBetBtnId.BET_BTN_ID_PLAYER_PAIR
    local player_tianwang = EBJLBetBtnId.BET_BTN_ID_PLAYER_TIANWANG
    local banker_duizi = EBJLBetBtnId.BET_BTN_ID_BANKER_PAIR
    local banker_tianwang = EBJLBetBtnId.BET_BTN_ID_BANKER_TIANWANG

    --闲天王（天王对）
    if player_result == player_tianwang or player_result == 1 then
        if banker_result == 1 or banker_result == banker_tianwang then
            if result_id == EBJLBetBtnId.BET_BTN_ID_BANKER then
                banker_points = 9
                player_points = 8
            elseif result_id == EBJLBetBtnId.BET_BTN_ID_PLAYER then
                banker_points = 8
                player_points = 9
            elseif banker_result == 1 or player_result == 1 then
                player_points = 8
                banker_points = 8
            else
                player_points = base.get_random(8,9)
                banker_points = player_points
            end
        else
            player_points = 8
            banker_points = base.get_random(0,7)
            if player_result ~= 1 then
                player_points = base.get_random(8,9)
            end
            if banker_result == banker_duizi then
                banker_points = banker_points - banker_points % 2
            end
        end
    --庄天王（天王对）
    elseif banker_result == banker_tianwang or banker_result == 1 then
        banker_points = 8
        player_points = base.get_random(0,7)
        if banker_result ~= 1 then
            banker_points = base.get_random(8,9)
        end
        if player_result == player_duizi then
            player_points = player_points - player_points % 2
        end
    --非天王
    else
        if player_result == player_duizi then
            if result_id == EBJLBetBtnId.BET_BTN_ID_PLAYER then
                player_points = 2 * base.get_random(1,3)
            elseif result_id == EBJLBetBtnId.BET_BTN_ID_BANKER 
            and banker_result == banker_duizi then
                player_points = 2 * base.get_random(0,2)
            else
                player_points = 2 * base.get_random(0,3)
            end
        end
        if banker_result == banker_duizi then
            if result_id == EBJLBetBtnId.BET_BTN_ID_PLAYER 
            and player_result == player_duizi then
                local r = math.floor(player_points / 2) - 1
                banker_points = 2 * base.get_random(0,r)
            elseif result_id == EBJLBetBtnId.BET_BTN_ID_BANKER 
            and player_result == player_duizi then
                local r = math.floor(player_points / 2) + 1
                banker_points = 2 * base.get_random(r,3)
            elseif result_id == EBJLBetBtnId.BET_BTN_ID_BANKER then
                 banker_points = 2 * base.get_random(1,3)
            else
                banker_points = 2 * base.get_random(0,3)
            end
        end
        if result_id == EBJLBetBtnId.BET_BTN_ID_BANKER then
            local r = (player_points or 0) + 1
            banker_points = banker_points or base.get_random(r,7)
            player_points = player_points or base.get_random(0,banker_points - 1)
        elseif result_id == EBJLBetBtnId.BET_BTN_ID_PLAYER then
            local r = (banker_points or 0) + 1
            player_points = player_points or base.get_random(r,7)
            banker_points = banker_points or base.get_random(0,player_points - 1)
        else
            local val = banker_points or player_points
            if not val then
                val = base.get_random(0,7)
            end
            banker_points = val
            player_points = val
        end
    end

    local banker_is_duizi = false
    if banker_result == 1 or banker_result == banker_duizi then
        banker_is_duizi = true
    end
    local player_is_duizi = false
    if player_result == 1 or player_result == player_duizi then
        player_is_duizi = true
    end

    --根据点数随机牌
    local player_cards = {}
    local banker_cards = {}
    local is_duizi = banker_is_duizi
    local total_points = banker_points
    for i = 1,4  do
        if i == 3 then
            is_duizi = player_is_duizi
            total_points = player_points
        end
        local ct = base.get_random(1,4)
        local cv = total_points
        if i % 2 ~= 0 then
            cv = base.get_random(0,total_points)
            if is_duizi then
                cv = math.floor(total_points / 2)
            elseif cv * 2 == total_points then
                cv = total_points
            end
        end
        total_points = total_points - cv
        if cv == 0 then
            cv = 10 + base.get_random(0,3)
        end
        local card = ct * 100 + cv
        if i < 3 then
            table.insert(banker_cards,card)
        else
            table.insert(player_cards,card)
        end
    end

    --总点数为0点时，去除对子或添加对子
    if banker_points == 0 then
        if banker_is_duizi then
            local ct = math.floor(banker_cards[2] / 100)
            local cv = banker_cards[1] % 100
            banker_cards[2] = ct * 100 + cv
        elseif banker_cards[1] % 100 == banker_cards[2] % 100 then
            local ct = math.floor(banker_cards[2] / 100)
            local cv = (banker_cards[2] % 100) + 1
            if cv > 13 then
                cv = 10
            end
            banker_cards[2] = ct * 100 + cv
        end
    end
    if player_points == 0 then
        if player_is_duizi then
            local ct = math.floor(player_cards[2] / 100)
            local cv = player_cards[1] % 100
            player_cards[2] = ct * 100 + cv
        elseif player_cards[1] % 100 == player_cards[2] % 100 then
            local ct = math.floor(player_cards[2] / 100)
            local cv = (player_cards[2] % 100) + 1
            if cv > 13 then
                cv = 10
            end
            player_cards[2] = ct * 100 + cv
        end
    end
    
    local is_fillcard = false

    --非天王补牌
    if player_points < 8 and banker_points < 8 then
        --处理闲家补牌
        local player_rules = Player_FillCard_Rules[player_points]
        if player_rules then
            local ct = base.get_random(1,4)
            local cv = 10 + base.get_random(0,3)
            local card = ct * 100 + cv
            table.insert(player_cards,card)
            is_fillcard = true
        end

        --处理庄家补牌
        local banker_rules = Banker_FillCard_Rules[banker_points]
        if banker_rules and not banker_rules[0] then
            local ct = base.get_random(1,4)
            local cv = 10 + base.get_random(0,3)
            local card = ct * 100 + cv
            table.insert(banker_cards,card)
            is_fillcard = true
        end
    end

    --返回结果
    local result_list = {
        banker_cards = banker_cards,
        player_cards = player_cards,
        banker_points = banker_points,
        player_points = player_points,
        prize_id = result_id,
        ext_prize_list = ext_prize_list,
        is_fillcard = is_fillcard,
    }

    return result_list
end

--
--获得牌型：天王、对子、天王对、普通牌
--0普通牌 1天王对 2对子 3天王 
--
function SlotTool.get_cards_type(cards)
    if #cards < 2 then
        return
    end

    local is_duizi = false
    local c1 = cards[1] % 100
    local c2 = cards[2] % 100
    if c1 == c2 then
        is_duizi = true
    end

    local is_tianwang = false
    local points = 0
    for _,v in pairs(cards) do
        local card = v % 100
        if v < 10 then
            points = (points + v) % 10
        end
    end
    if points >= 8 then
        is_tianwang = true
    end

    local is_tiandui = false
    if is_duizi and is_tianwang then
        is_tiandui = true
    end

    local result = 0
    if is_tiandui then
        result = 1
    elseif is_duizi then
        result = 2
    elseif is_tianwang then
        result = 3
    end

    return result
end

--
--处理补牌
--
function SlotTool.handle_fill_card(banker_cards, player_cards, banker_points, player_points, result_id)
    --获得随机牌
    local function get_random_card(cards)
        if #cards <= 0 then return end
        local ct = base.get_random(1,4)
        local r = base.get_random(1,#cards)
        local cv = cards[r]
        return (ct * 100 + cv),cv
    end

    --------------------

    local flag = 0
    if result_id == EBJLBetBtnId.BET_BTN_ID_PLAYER then         --闲赢
        flag = -1
    elseif result_id == EBJLBetBtnId.BET_BTN_ID_BANKER then     --庄赢
        flag = 1
    end

    --任一人拿到天王牌，不补牌
    if banker_points > 7 or player_points > 7 then
        if (flag == 0 and banker_points == player_points)
        or (flag == 1 and banker_points > player_points)
        or (flag == -1 and banker_points < player_points) then
            return true,{banker_points,player_points}
        else
            return false
        end
    end

    local flags = flag
    if banker_points < 3 then
        flag = 2
    end

    --闲家补牌
    local player_rules = Player_FillCard_Rules[player_points]
    if player_rules then
        local cards = { }
        for i = 1,13  do
            local point = (i > 9 and 0) or i
            local total_points = (player_points + point) % 10
            if flag == -1 and (total_points > banker_points) then
                table.insert(cards,i)
            elseif falg == 1 and (total_points < banker_points) then
                table.insert(cards,i)
            elseif flag == 0 and (total_points == banker_points) then
                table.insert(cards,i)
            elseif flag == 2 and (total_points > 0) then
                table.insert(cards,i)
            end
        end
        local card,cv = get_random_card(cards)
        if not card then
            return false
        else
            if cv < 10 then
                player_points = (player_points + cv) % 10
            end
            table.insert(player_cards,card)
        end
    end

    --庄家补牌
    local key = -1
    if player_cards[3] then
        key = player_cards[3] % 100
        if key > 9 then key = 0 end
    end
    local banker_rules = Banker_FillCard_Rules[banker_points]
    if banker_rules and not banker_rules[key] then
        local cards = { }
        for i = 1,13  do
            local point = (i > 9 and 0) or i
            local total_points = (banker_points + point) % 10
            if flags == -1 and (total_points < player_points) then
                table.insert(cards,i)
            elseif flags == 1 and (total_points > player_points) then
                table.insert(cards,i)
            elseif flags == 0 and (total_points == player_points) then
                table.insert(cards,i)
            end
        end
        local card,cv = get_random_card(cards)
        if not card then
            return false 
        else
            if cv < 10 then
                banker_points = (banker_points + cv) % 10
            end
            table.insert(banker_cards,card)
        end
    end

    --判断是否符合最终结果
    if (flags == 0 and banker_points == player_points)
    or (flags == 1 and banker_points > player_points)
    or (flags == -1 and banker_points < player_points) then
        return true,{banker_points,player_points}
    end

    return false
end

--
--尝试获取纯随机结果
--
function SlotTool.try_get_real_random_result(result_id, ext_prize_list, spe_cards_list)
    local banker_result = spe_cards_list[1]
    local player_result = spe_cards_list[2]
    local player_duizi = EBJLBetBtnId.BET_BTN_ID_PLAYER_PAIR
    local player_tianwang = EBJLBetBtnId.BET_BTN_ID_PLAYER_TIANWANG
    local banker_duizi = EBJLBetBtnId.BET_BTN_ID_BANKER_PAIR
    local banker_tianwang = EBJLBetBtnId.BET_BTN_ID_BANKER_TIANWANG

    --获取纯随机结果
    local result = SlotTool.get_real_random_result()

    local is_ok = false
    if result.prize_id == result_id 
    and #result.ext_prize_list == #ext_prize_list then
        --检测牌型
        local banker_flag = false
        local player_flag = false
        local banker_type = SlotTool.get_cards_type(result.banker_cards)
        local player_type = SlotTool.get_cards_type(result.player_cards)
        if (player_type == 0 and player_result == 0) 
        or (player_type == 1 and player_result == 1)
        or (player_type == 2 and player_result == player_duizi)
        or (player_type == 3 and player_result == player_tianwang) then
            player_flag = true
        end
        if (banker_type == 0 and banker_result == 0) 
        or (banker_type == 1 and banker_result == 1)
        or (banker_type == 2 and banker_result == banker_duizi)
        or (banker_type == 3 and banker_result == banker_tianwang) then
            banker_flag = true
        end
        if player_flag and banker_flag then
            is_ok = true
        end
    end

    if is_ok then
        return result
    end

    --不符合结果，存到预存牌堆
    local card_list = Profile_Cards_List[result.prize_id]
    if card_list and card_list[banker_result] 
    and card_list[banker_result][player_result] then
        local target_list = card_list[banker_result][player_result]
        if #target_list >= 20 then
            card_list[banker_result][player_result] = { }
            target_list = card_list[banker_result][player_result]
        end
        local item = {
            banker_cards = result.banker_cards,
            player_cards = result.player_cards,
        }
        table.insert(target_list,item)
    end

    return false
end

--
--获得随机和牌结果
--
function SlotTool.get_random_tie_result()
    local banker_points = 0
    local player_points = 0
    local banker_cards = {}
    local player_cards = {}
    local is_fillcard = false

    --生成闲前2张牌
    for i = 1,2 do
        local ct = base.get_random(1,4)
        local cv = base.get_random(1,13)
        table.insert(player_cards,ct * 100 + cv)
        if cv < 10 then
            player_points = (player_points + cv) % 10
        end
    end

    --处理闲天王
    if player_points > 7 then
        for j = 1,2 do
            if j == 1 then
                local ct = base.get_random(1,4)
                local cv = base.get_random(1,13)
                table.insert(banker_cards,ct * 100 + cv)
                if cv < 10 then
                    banker_points = (banker_points + cv) % 10
                end
            else
                local ct = base.get_random(1,4)
                local cv = player_points - banker_points
                if cv == -1 then cv = 9
                elseif cv == 0 then cv = base.get_random(10,13) end
                table.insert(banker_cards,ct * 100 + cv)
                if cv < 10 then
                    banker_points = (banker_points + cv) % 10
                end 
            end
        end
    else
        --闲补牌
        if Player_FillCard_Rules[player_points] then
            local ct = base.get_random(1,4)
            local cv = base.get_random(1,13)
            table.insert(player_cards,ct * 100 + cv)
            if cv < 10 then
                player_points = (player_points + cv) % 10
            end
            is_fillcard = true
        end

        --生成庄家牌
        local key = -1
        if player_cards[3] then
            key = player_cards[3] % 100
            if key > 9 then key = 0 end
        end
        for j = 1,2 do
            if j == 1 then
                local ct = base.get_random(1,4)
                local cv = base.get_random(1,13)
                table.insert(banker_cards,ct * 100 + cv)
                if cv < 10 then
                    banker_points = (banker_points + cv) % 10
                end
            else
                local cvs = {}
                for k = 1,13 do
                    local total_points = banker_points
                    if k < 10 then
                        total_points = (total_points + k) % 10
                    end
                    if (total_points < 8 and total_points == player_points) then
                        table.insert(cvs, k)
                    elseif total_points < 8 then
                        local banker_rules = Banker_FillCard_Rules[total_points]
                        if banker_rules and not banker_rules[key] then
                            table.insert(cvs, k)
                        end
                    end
                end
                if #cvs > 0 then
                    local ct = base.get_random(1,4)
                    local r = base.get_random(1,#cvs)
                    local cv = cvs[r]
                    table.insert(banker_cards,ct * 100 + cv)
                    if cv < 10 then
                        banker_points = (banker_points + cv) % 10
                    end
                else
                    return false
                end
            end
        end

        --庄补牌
        local banker_rules = Banker_FillCard_Rules[banker_points]
        if banker_rules and not banker_rules[key] then
            local ct = base.get_random(1,4)
            local cv = player_points - banker_points
            if cv < 0 then cv = cv + 10 end
            if cv == 0 then cv = base.get_random(10,13) end
            table.insert(banker_cards,ct * 100 + cv)
            if cv < 10 then
                banker_points = (banker_points + cv) % 10
            end
            is_fillcard = true
        end

    end

    --判定开牌结果
    local ext_prize_list = {}
    if banker_points ~= player_points then
        return false
    end

    --额外奖励
    if banker_points >= 8 then
        table.insert(ext_prize_list, EBJLBetBtnId.BET_BTN_ID_BANKER_TIANWANG)
    end
    if player_points >= 8 then
        table.insert(ext_prize_list, EBJLBetBtnId.BET_BTN_ID_PLAYER_TIANWANG)
    end
    if banker_cards[1] % 100 == banker_cards[2] % 100 then
        table.insert(ext_prize_list, EBJLBetBtnId.BET_BTN_ID_BANKER_PAIR)
    end
    if player_cards[1] % 100 == player_cards[2] % 100 then
        table.insert(ext_prize_list, EBJLBetBtnId.BET_BTN_ID_PLAYER_PAIR)
    end

    --返回结果
    local result_list = {
        banker_cards = banker_cards,
        player_cards = player_cards,
        banker_points = banker_points,
        player_points = player_points,
        prize_id = EBJLBetBtnId.BET_BTN_ID_TIE,
        ext_prize_list = ext_prize_list,
        is_fillcard = is_fillcard,
    }

    return result_list
end

--
--根据开奖结果随机牌,没有特殊奖励
--
function SlotTool.get_random_cards_by_result(result_id, ext_prize_list, spe_cards_list)
    if result_id == EBJLBetBtnId.BET_BTN_ID_TIE then
        local cnt = 0
        while cnt < 5 do
            local result = SlotTool.get_random_tie_result()
            if result and result.prize_id == result_id then
                return result
            end
            cnt = cnt + 1
        end
    else
        local cnt = 0
        while cnt < 20 do
            local result = SlotTool.get_real_random_result()
            if result.prize_id == result_id then
                return result
            end
            cnt = cnt + 1
        end
    end

    return SlotTool.get_random_cards_by_result_other(result_id, ext_prize_list, spe_cards_list)
end

--
--根据开奖结果，获得庄闲手牌
--一共8副牌，前2张牌纯随机，第3,4张根据开奖结果决定；需要补牌则根据开奖结果补牌
--
function SlotTool.get_random_cards_by_result_other(result_id, ext_prize_list, spe_cards_list)
    local indexs = {}

    --随机获得一张不重复牌
    local function get_random_card_from_cardstack(indexs,cards,card_num)
        indexs = indexs or {}
        cards = cards or CardData
        card_num = #cards * CardPairNum
        local card = nil
        while true do
            local r = base.get_random(1,card_num)
            if not indexs[r] then
                indexs[r] = true
                r = (r - 1) % #cards + 1
                card = cards[r]
                break
            end
        end
        return card
    end

    ----------------------

    --尝试获取纯随机结果
    local result = SlotTool.try_get_real_random_result(result_id,ext_prize_list,spe_cards_list)
    if result then
        return result
    end

    local player_cards = {}
    local banker_cards = {}
    local player_points = 0
    local banker_points = 0
    local banker_result = spe_cards_list[1]
    local player_result = spe_cards_list[2]
    local player_duizi = EBJLBetBtnId.BET_BTN_ID_PLAYER_PAIR
    local player_tianwang = EBJLBetBtnId.BET_BTN_ID_PLAYER_TIANWANG
    local banker_duizi = EBJLBetBtnId.BET_BTN_ID_BANKER_PAIR
    local banker_tianwang = EBJLBetBtnId.BET_BTN_ID_BANKER_TIANWANG

    --非特殊牌，随机发第1张，去除特殊牌后发第2张
    if banker_result == 0 then
        local card1 = get_random_card_from_cardstack(indexs)
        local point1 = card1 % 100
        if point1 >= 10 then point1 = 0 end
        local cnt = 0
        local card2,point2
        while true do
            card2 = get_random_card_from_cardstack(indexs)
            point2 = card2 % 100
            if point2 >= 10 then point2 = 0 end
            local total_points = (point1 + point2) % 10
            if (total_points < 8) and (card2 % 100 ~= card1 % 100) then
                break
            end
            if cnt > 10 then
                card1 = 102; point1 = 2
                card2 = 203; point2 = 3
                break
            end
            cnt = cnt + 1
        end
        banker_cards = {card1,card2}
        banker_points = (point1 + point2) % 10
    end
    if player_result == 0 then
        local card1 = get_random_card_from_cardstack(indexs)
        local point1 = card1 % 100
        if point1 >= 10 then point1 = 0 end
        local cnt = 0
        local card2,point2
        while true do
            card2 = get_random_card_from_cardstack(indexs)
            point2 = card2 % 100
            if point2 >= 10 then point2 = 0 end
            local total_points = (point1 + point2) % 10
            if (total_points < 8) and (card2 % 100 ~= card1 % 100) then
                break
            end
            if cnt > 10 then
                card1 = 404; point1 = 4
                card2 = 310; point2 = 0
                break
            end
            cnt = cnt + 1
        end
        player_cards = {card1,card2}
        player_points = (point1 + point2) % 10
    end

    --对子，则只需产生一张牌,不能为4 9
    if banker_result == EBJLBetBtnId.BET_BTN_ID_BANKER_PAIR then
        local cards = {10,11,12,13,1,2,3,5,6,7,8}
        local cv1 = get_random_card_from_cardstack({},cards)
        local ct1 = base.get_random(1,4)
        local ct2 = base.get_random(1,4)
        local card1 = ct1 * 100 + cv1
        local card2 = ct2 * 100 + cv1
        local point = (cv1 * 2) % 10
        if cv1 >= 10 then point = 0 end
        banker_points = point
        banker_cards = {card1,card2}
    end
    if player_result == EBJLBetBtnId.BET_BTN_ID_PLAYER_PAIR then
        local cards = {10,11,12,13,1,2,3,5,6,7,8}
        local cv1 = get_random_card_from_cardstack({},cards)
        local ct1 = base.get_random(1,4)
        local ct2 = base.get_random(1,4)
        local card1 = ct1 * 100 + cv1
        local card2 = ct2 * 100 + cv1
        local point = (cv1 * 2) % 10
        if cv1 >= 10 then point = 0 end
        player_points = point
        player_cards = {card1,card2}
    end

    --天王，点数只可能为8或9，2张牌只能为0,8 1,7 2,6 3,5 或 0,9 1,8 2,7 3,6 4,5
    if banker_result == EBJLBetBtnId.BET_BTN_ID_BANKER_TIANWANG then
        local cards = {{10,11,12,13,1,2,3},{10,11,12,13,1,2,3,4}}
        local idx = base.get_random(1,2)
        local ct1 = base.get_random(1,4)
        local ct2 = base.get_random(1,4)
        local cv1 = get_random_card_from_cardstack({},cards[idx])
        local card1 = ct1 * 100 + cv1
        local card2 = ct2 * 100
        local real_cv = (cv1 < 10 and cv1) or 0
        if idx == 1 then
            banker_points = 8
            card2 = card2 + (8 - real_cv)
        else
            banker_points = 9
            card2 = card2 + (9 - real_cv)
        end
        banker_cards = {card1,card2}
    end
    if player_result == EBJLBetBtnId.BET_BTN_ID_PLAYER_TIANWANG then
        local cards = {{10,11,12,13,1,2,3},{10,11,12,13,1,2,3,4}}
        local idx = base.get_random(1,2)
        local ct1 = base.get_random(1,4)
        local ct2 = base.get_random(1,4)
        local cv1 = get_random_card_from_cardstack({},cards[idx])
        local card1 = ct1 * 100 + cv1
        local card2 = ct2 * 100
        local real_cv = (cv1 < 10 and cv1) or 0
        if idx == 1 then
            player_points = 8
            card2 = card2 + (8 - real_cv)
        else
            player_points = 9
            card2 = card2 + (9 - real_cv)
        end
        player_cards = {card1,card2}
    end

    --天王对，前2张为对子，总点数大于7
    if banker_result == 1 then
        local cards = {10,11,12,13,1,2,4,5,6,7,9}
        local cv1 = get_random_card_from_cardstack({},cards)
        local ct1 = base.get_random(1,4)
        local ct2 = base.get_random(1,4)
        local card1 = ct1 * 100 + cv1
        local card2 = ct2 * 100 + cv1
        local point = (cv1 * 2) % 10
        if cv1 >= 10 then point = 0 end
        banker_points = point
        banker_cards = {card1,card2}
    end
    if player_result == 1 then
        local cards = {10,11,12,13,1,2,4,5,6,7,9}
        local cv1 = get_random_card_from_cardstack({},cards)
        local ct1 = base.get_random(1,4)
        local ct2 = base.get_random(1,4)
        local card1 = ct1 * 100 + cv1
        local card2 = ct2 * 100 + cv1
        local point = (cv1 * 2) % 10
        if cv1 >= 10 then point = 0 end
        player_points = point
        player_cards = {card1,card2}
    end

    --处理补牌
    local is_ok,allpoints = SlotTool.handle_fill_card(banker_cards,player_cards,banker_points,player_points,result_id)
    if is_ok then
        --检测牌型
        local banker_flag = false
        local player_flag = false
        local banker_type = SlotTool.get_cards_type(banker_cards)
        local player_type = SlotTool.get_cards_type(player_cards)
        if (player_type == 0 and player_result == 0) 
        or (player_type == 1 and player_result == 1)
        or (player_type == 2 and player_result == player_duizi)
        or (player_type == 3 and player_result == player_tianwang) then
            player_flag = true
        end
        if (banker_type == 0 and banker_result == 0) 
        or (banker_type == 1 and banker_result == 1)
        or (banker_type == 2 and banker_result == banker_duizi)
        or (banker_type == 3 and banker_result == banker_tianwang) then
            banker_flag = true
        end
        if not player_flag or not banker_flag  then
            is_ok = false
        end
    end

    --生成牌失败，从预存牌中取
    if not is_ok then
        return SlotTool.get_cards_from_profile_cards(result_id,ext_prize_list,player_result,banker_result)
    end

    local is_fillcard = false
    if #player_cards > 2 or #banker_cards > 2 then
        is_fillcard = true
    end

    local result_list = {
        banker_cards = banker_cards,
        player_cards = player_cards,
        banker_points = allpoints[1],
        player_points = allpoints[2],
        is_fillcard = is_fillcard,
        prize_id = result_id,
        ext_prize_list = ext_prize_list,
    }

    return result_list
end

--
--获得系统输钱上限
--
function SlotTool.get_system_lose_limit(gamename)
    local loselimit = 0
    local S = SlotTool.get_static_inventory_from_redis(gamename)
    if S < 0 then
        loselimit = SystemLoseLimit_Base
    else
        loselimit = math.floor(S * SystemLoseLimit_Factor) + SystemLoseLimit_Base
    end

    return loselimit
end

--
--获取特殊牌奖励
--庄：庄对：闲对;  庄天王：闲天王、闲对、闲天王对;    庄天王对：闲对
--闲：庄对：闲天王、闲天王对、闲对;     庄天王：闲天王;         庄天王对：闲天王
--平：庄对：闲对;  庄天王：闲天王、闲天王对;   庄天王对：闲天王、闲天王对
--
function SlotTool.get_special_cards_prize(result_id, total_bet_list, gamename, total_bet, is_robot_banker)
    local filters = {
        [1101] = {  --庄
            [1104] = {[1105] = true, [1107] = false, [1] = false},  --庄对
            [1106] = {[1105] = true, [1107] = true, [1] = true},    --庄天王
            [1]    = {[1105] = true, [1107] = false, [1] = false},  --庄天王对
            [0]    = {[1105] = true, [1107] = false, [1] = false},  --非特殊
        },
        [1102] = {  --闲
            [1104] = {[1105] = true, [1107] = true, [1] = true},    --庄对
            [1106] = {[1105] = false, [1107] = true, [1] = false},  --庄天王
            [1]    = {[1105] = false, [1107] = true, [1] = false},  --庄天王对
            [0]    = {[1105] = true, [1107] = true, [1] = true},    --非特殊
        },
        [1103] = {  --平
            [1104] = {[1105] = true, [1107] = false, [1] = false},  --庄对
            [1106] = {[1105] = false, [1107] = true, [1] = true},   --庄天王
            [1]    = {[1105] = false, [1107] = true, [1] = true},   --庄天王对
            [0]    = {[1105] = true, [1107] = false, [1] = false},  --非特殊
        },
    }

    --检测是否小于输钱上限
    local function  check_is_less_loselimit(result_id, ext_result_list, total_bet_list, total_bet, loselimit, is_robot_banker)
        total_bet = total_bet - Odds_list[result_id] * total_bet_list[result_id]
        if result_id == EBJLBetBtnId.BET_BTN_ID_TIE then
            local bet_id_banker = EBJLBetBtnId.BET_BTN_ID_BANKER
            local bet_id_player = EBJLBetBtnId.BET_BTN_ID_PLAYER
            total_bet = total_bet - ( total_bet_list[bet_id_banker] + total_bet_list[bet_id_player] )
        end
        for _,id in pairs(ext_result_list) do
            local bet_id = id
            total_bet = total_bet - Odds_list[id] * total_bet_list[bet_id]
        end
        if is_robot_banker and total_bet < -loselimit then
            return false
        elseif not is_robot_banker and total_bet > loselimit then
            return false
        end
        return true
    end

    ---------------------------------------

    local loselimit = SlotTool.get_system_lose_limit(gamename)
    local ext_prize_list = {}

    local banker_result = SlotTool.get_random_result_by_weightlist(Weight_list[2])
    if banker_result == 1 then
        table.insert(ext_prize_list,EBJLBetBtnId.BET_BTN_ID_BANKER_TIANWANG)
        table.insert(ext_prize_list,EBJLBetBtnId.BET_BTN_ID_BANKER_PAIR)
    elseif banker_result > 0 then
        table.insert(ext_prize_list,banker_result)
    end

    --检测是否会超系统输钱上限
    local is_ok = check_is_less_loselimit(result_id,ext_prize_list,total_bet_list,total_bet,loselimit,is_robot_banker)
    if not is_ok then
        banker_result = 0
        ext_prize_list = {}
    end

    local tmp_prize_list = tabletool.deepcopy(ext_prize_list)
    local filter = filters[result_id][banker_result]
    local player_result = SlotTool.get_random_result_by_weightlist(Weight_list[3])
    if filter and filter[player_result] then
        if player_result == 1 then
            table.insert(ext_prize_list,EBJLBetBtnId.BET_BTN_ID_PLAYER_TIANWANG)
            table.insert(ext_prize_list,EBJLBetBtnId.BET_BTN_ID_PLAYER_PAIR)
        elseif player_result > 0 then
            table.insert(ext_prize_list,player_result)
        end
    else
        player_result = 0
    end

    --检测是否会超系统输钱上限
    is_ok = check_is_less_loselimit(result_id,ext_prize_list,total_bet_list,total_bet,loselimit,is_robot_banker)
    if not is_ok then
        player_result = 0
        ext_prize_list = tmp_prize_list
    end

    return ext_prize_list,{banker_result,player_result}
end

--
--获取系统最优开奖随机结果
--
function SlotTool.get_best_random_result(sys_win_list, total_bet_list, is_robot_banker)
    if sys_win_list and #sys_win_list > 1 then
        local win_w_list = {}
        local total_weight = 0
        for _,id in pairs(sys_win_list) do
            win_w_list[id] = Weight_list[1][id]
            if id == EBJLBetBtnId.BET_BTN_ID_TIE and win_w_list[id] >= 2 then
                win_w_list[id] = math.floor(win_w_list[id] / 2)
            end
            total_weight = total_weight + win_w_list[id]
        end
        return SlotTool.get_random_result_by_weightlist(win_w_list,total_weight)
    else
        return SlotTool.get_min_spin_result(total_bet_list,is_robot_banker)
    end
end

--
--获取系统较差开奖随机结果
--去掉系统超上限亏损项和系统最优开奖（返钱数最小）
--
function SlotTool.get_bad_random_result(sys_lose_list, total_bet_list, gamename, total_bet, is_robot_banker)
    local loselimit = SlotTool.get_system_lose_limit(gamename)
    local random_id_list = tabletool.deepcopy(Weight_list[1])
    local best_id = SlotTool.get_min_spin_result(total_bet_list,is_robot_banker)

    --去除超上限亏损
    local max_id = 0
    if sys_lose_list and #sys_lose_list > 0 then
        local max_ksz = 0
        for _,id in pairs(sys_lose_list) do
            local bet_id = id
            local rtm = Odds_list[id] * total_bet_list[bet_id]
            if bet_id == EBJLBetBtnId.BET_BTN_ID_TIE then
                rtm = rtm + total_bet - total_bet_list[bet_id]
            end
            local ksz =  total_bet - rtm
            if is_robot_banker and ksz < -loselimit then
                random_id_list[id] = nil
            elseif not is_robot_banker and ksz > loselimit then
                random_id_list[id] = nil
            end
            if max_ksz < math.abs(ksz) then
                max_ksz = math.abs(ksz)
                max_id = id
            end
        end
    end
    random_id_list[best_id] = nil

    --开奖列表为空，返回系统亏损第二大的
    if next(random_id_list) == nil then
        local val = 0
        local ret_id = best_id
        if sys_lose_list and #sys_lose_list > 1 then
            for _,id in pairs(sys_lose_list) do
                if id ~= max_id then
                    local bet_id = id
                    local rtm = Odds_list[id] * total_bet_list[bet_id]
                    if bet_id == EBJLBetBtnId.BET_BTN_ID_TIE then
                        rtm = rtm + total_bet - total_bet_list[bet_id]
                    end
                    local ksz = total_bet - rtm
                    if val < math.abs(ksz) then
                        val = math.abs(ksz)
                        ret_id = id
                    elseif ret_id == EBJLBetBtnId.BET_BTN_ID_TIE 
                    and val == math.abs(ksz) then
                        val = math.abs(ksz)
                        ret_id = id
                    end
                end
            end
        end
        return ret_id
    end

    --只有2个开奖项，减少平概率
    local num = 0
    local tie_id = EBJLBetBtnId.BET_BTN_ID_TIE
    for k,v in pairs(random_id_list) do
        num = num + 1
    end
    if num == 2 and random_id_list[tie_id] then
        random_id_list[tie_id] = math.floor(random_id_list[tie_id] / 5)
    end

    return SlotTool.get_random_result_by_weightlist(random_id_list)
end

--
--获取普通随机结果
--去掉系统超上限亏损项
--
function SlotTool.get_normal_random_result(sys_lose_list, total_bet_list, gamename, total_bet, is_robot_banker)
    local loselimit = SlotTool.get_system_lose_limit(gamename)
    local random_id_list = tabletool.deepcopy(Weight_list[1])

    --去除超上限亏损
    if sys_lose_list and #sys_lose_list > 0 then
        for _,id in pairs(sys_lose_list) do
            local bet_id = id
            local rtm = Odds_list[id] * total_bet_list[bet_id]
            if bet_id == EBJLBetBtnId.BET_BTN_ID_TIE then
                rtm = rtm + total_bet - total_bet_list[bet_id]
            end
            local ksz =  total_bet - rtm
            if is_robot_banker and ksz < -loselimit then
                random_id_list[id] = nil
            elseif not is_robot_banker and ksz > loselimit then
                random_id_list[id] = nil
            end
        end
    end

    --所有项超上限，则返回最小的
    if next(random_id_list) == nil then
        return SlotTool.get_min_spin_result(total_bet_list,is_robot_banker)
    end

    --只有2个开奖项，减少平概率
    local num = 0
    local tie_id = EBJLBetBtnId.BET_BTN_ID_TIE
    for k,v in pairs(random_id_list) do
        num = num + 1
    end
    if num == 2 and random_id_list[tie_id] then
        random_id_list[tie_id] = math.floor(random_id_list[tie_id] / 5)
    end

    --产生随机结果
    return SlotTool.get_random_result_by_weightlist(random_id_list)
end

--
--获得纯随机结果
--
function SlotTool.get_real_random_result()
    local banker_cards = {}
    local player_cards = {}
    local card_num = #CardData * CardPairNum
    local indexs = {}
    local banker_points = 0
    local player_points = 0

    --随机获得一张不重复牌
    local function get_random_card_from_cardstack(card_num,indexs)
        indexs = indexs or {}
        local card = nil
        while true do
            local r = base.get_random(1,card_num)
            if not indexs[r] then
                indexs[r] = true
                r = (r - 1) % #CardData + 1
                card = CardData[r]
                break
            end
        end

        return card
    end

    --随机发4张
    for i = 1,4 do
        local card = get_random_card_from_cardstack(card_num,indexs)
        local val = card % 100
        if i % 2 == 0 then  --2,4张发给庄家
            table.insert(banker_cards,card)
            if val < 10 then
                banker_points = (banker_points + val) % 10
            end
        else
            table.insert(player_cards,card)
            if val < 10 then
                player_points = (player_points + val) % 10
            end
        end
    end
	
    --处理补牌
    local is_fillcard = false
    if banker_points < 8 and player_points < 8 then
        --闲家补牌
        is_fillcard = Player_FillCard_Rules[player_points]
        if is_fillcard then --随机补一张牌
            local card = get_random_card_from_cardstack(card_num,indexs)
            table.insert(player_cards,card)
            local val = card % 100
            if val < 10 then
                player_points = (player_points + val) % 10
            end
        end

        --庄家补牌
        local key = -1
        if player_cards[3] then
            key = player_cards[3] % 100
            if key > 9 then key = 0 end
        end
        local banker_rules = Banker_FillCard_Rules[banker_points]
        if banker_rules and not banker_rules[key] then
            local card = get_random_card_from_cardstack(card_num,indexs)
            table.insert(banker_cards,card)
            local val = card % 100
            if val < 10 then
                banker_points = (banker_points + val) % 10
            end
            is_fillcard = true
        end
    end

    --判定开牌结果
    local prize_id = nil
    local ext_prize_list = {}
    if banker_points > player_points then
        prize_id = EBJLBetBtnId.BET_BTN_ID_BANKER
    elseif banker_points < player_points then
        prize_id = EBJLBetBtnId.BET_BTN_ID_PLAYER
    else
        prize_id = EBJLBetBtnId.BET_BTN_ID_TIE
    end

    --额外奖励
    if banker_points >= 8 then
        table.insert(ext_prize_list, EBJLBetBtnId.BET_BTN_ID_BANKER_TIANWANG)
    end
    if player_points >= 8 then
        table.insert(ext_prize_list, EBJLBetBtnId.BET_BTN_ID_PLAYER_TIANWANG)
    end
    if banker_cards[1] % 100 == banker_cards[2] % 100 then
        table.insert(ext_prize_list, EBJLBetBtnId.BET_BTN_ID_BANKER_PAIR)
    end
    if player_cards[1] % 100 == player_cards[2] % 100 then
        table.insert(ext_prize_list, EBJLBetBtnId.BET_BTN_ID_PLAYER_PAIR)
    end
    
    --返回结果
    local result_list = {
        banker_cards = banker_cards,
        player_cards = player_cards,
        banker_points = banker_points,
        player_points = player_points,
        prize_id = prize_id,
        ext_prize_list = ext_prize_list,
        is_fillcard = is_fillcard,
    }

    return result_list
end

--
--产生一个随机旋转结果
--
function SlotTool.gen_random_spin_result(gamename, total_bet_list, total_bet, avg_bet, is_robot_banker, is_random)
    local result_id = nil

     --计算系统输赢列表
    local sys_win_list,sys_lose_list = SlotTool.get_system_win_lose_list(total_bet_list,total_bet,is_robot_banker)

    --随机开奖
    if is_random then
        result_id = SlotTool.get_normal_random_result(sys_lose_list,total_bet_list,gamename,total_bet,is_robot_banker)
    else
        --获取开奖类型,分：系统最优开奖，系统较差开奖，随机开奖3种
        local lottery_type = SlotTool.get_lottery_type(gamename)
        if lottery_type == LotteryType.LOTTERY_TYPE_BEST then     --最优开奖
            result_id = SlotTool.get_best_random_result(sys_win_list,total_bet_list,is_robot_banker)
        elseif lottery_type == LotteryType.LOTTERY_TYPE_BAD then  --较差开奖
            result_id = SlotTool.get_bad_random_result(sys_lose_list,total_bet_list,gamename,total_bet,is_robot_banker)
        else    --随机开奖
            result_id = SlotTool.get_normal_random_result(sys_lose_list,total_bet_list,gamename,total_bet,is_robot_banker)
        end
    end

    --获取特殊牌奖励
    local ext_prize_list = { }
    local spe_cards_list = {0,0}
    if lottery_type ~= LotteryType.LOTTERY_TYPE_BEST then
        ext_prize_list,spe_cards_list = SlotTool.get_special_cards_prize(result_id,total_bet_list,gamename,total_bet,is_robot_banker)
    end

    --根据开牌结果获取随机牌
    local result_list = SlotTool.get_random_cards_by_result(result_id,ext_prize_list,spe_cards_list)

    --无特殊奖励（开放庄对，闲对；庄天王，闲天王暂不开放）
    if result_list.ext_prize_list then
        local real_ext_prize_list = {}
        for k,v in pairs(result_list.ext_prize_list) do
            if v == EBJLBetBtnId.BET_BTN_ID_BANKER_PAIR
            or v == EBJLBetBtnId.BET_BTN_ID_PLAYER_PAIR then
                table.insert(real_ext_prize_list, v)
            end
        end
        result_list.ext_prize_list = real_ext_prize_list
    end

    return result_list
end

--
--获取最小奖励
--
function SlotTool.get_min_spin_result(total_bet_list, is_robot_banker)
    local min_id = 0
    local min_rtm = 100000000000
    if not is_robot_banker then
        min_rtm = 0
    end
    
    --循环每一种情况,求返钱数最小的
    for id,_ in pairs(Weight_list[1]) do
        local bet_id = id
        local odds = Odds_list[id]
        if odds > 0 and total_bet_list[bet_id] then
            local rtm = odds * total_bet_list[bet_id]   --返钱数
            if bet_id == EBJLBetBtnId.BET_BTN_ID_TIE then
                local bet_id_banker = EBJLBetBtnId.BET_BTN_ID_BANKER
                local bet_id_player = EBJLBetBtnId.BET_BTN_ID_PLAYER
                rtm = rtm + total_bet_list[bet_id_banker] + total_bet_list[bet_id_player]
            end
            if is_robot_banker then
                if min_rtm > rtm then
                    min_rtm = rtm
                    min_id = id
                elseif min_rtm == rtm 
                and min_id == EBJLBetBtnId.BET_BTN_ID_TIE then
                    min_rtm = rtm
                    min_id = id
                end
            else
                if min_rtm < rtm then
                    min_rtm = rtm
                    min_id = id
                elseif min_rtm == rtm 
                and min_id == EBJLBetBtnId.BET_BTN_ID_TIE then
                    min_rtm = rtm
                    min_id = id
                end
            end
        end
    end
    
    return min_id
end

--
--获取最大奖励
--
function SlotTool.get_max_spin_result(total_bet_list, is_robot_banker)
    local max_id = 0
    local max_rtm = 0
    if not is_robot_banker then
        max_rtm = 100000000000
    end
    
    --循环每一种情况,求返钱数最大的
    for id,_ in pairs(Weight_list[1]) do
        local bet_id = id
        local odds = Odds_list[id]
        if odds > 0 and total_bet_list[bet_id] then
            local rtm = odds * total_bet_list[bet_id]   --返钱数
            if bet_id == EBJLBetBtnId.BET_BTN_ID_TIE then
                local bet_id_banker = EBJLBetBtnId.BET_BTN_ID_BANKER
                local bet_id_player = EBJLBetBtnId.BET_BTN_ID_PLAYER
                rtm = rtm + total_bet_list[bet_id_banker] + total_bet_list[bet_id_player]
            end
            if is_robot_banker then
                if max_rtm < rtm then
                    max_rtm = rtm
                    max_id = id
                end
            else
                if max_rtm > rtm then
                    max_rtm = rtm
                    max_id = id
                end
            end
        end
    end
    
    return max_id
end


--
--检测随机结果的正确性
--
function SlotTool.check_random_spin_result(spin_result)
    if not spin_result or not spin_result.banker_cards or not spin_result.player_cards 
    or #(spin_result.banker_cards) < 2 or #(spin_result.player_cards) < 2 then
        return false
    end

    --检测是否有同牌
    local flags = {}
    local max_same = CardPairNum
    for _,card in pairs(spin_result.banker_cards) do
        if flags[card] == max_same then
            filelog.sys_error("-----------have same card------",spin_result)
            return false
        end
        flags[card] = (flags[card] or 0) + 1
    end
    for _,card in pairs(spin_result.player_cards) do
        if flags[card] == max_same then
            filelog.sys_error("-----------have same card------",spin_result)
            return false
        end
        flags[card] = (flags[card] or 0) + 1
    end

    --检测补牌规则是否正确
    local banker_points = 0
    local player_points = 0
    for i = 1,2 do
        local bcv = (spin_result.banker_cards[i]) % 100
        if bcv < 10 then
            banker_points = (banker_points + bcv) % 10
        end
        local pcv = (spin_result.player_cards[i]) % 100
        if pcv < 10 then
            player_points = (player_points + pcv) % 10
        end
    end
    
    --闲补牌
    local isok1 = false
    if ((player_points > 7 or banker_points > 7) and not spin_result.player_cards[3])
    or (Player_FillCard_Rules[player_points] and spin_result.player_cards[3])
    or (not Player_FillCard_Rules[player_points] and not spin_result.player_cards[3]) then
        isok1 = true
    end
    if not isok1 then
        filelog.sys_error("-----------player fill card rule error ------",spin_result)
        return false
    end

    --庄补牌
    local key = -1
    if spin_result.player_cards[3] then
        key = spin_result.player_cards[3] % 100
        if key > 9 then key = 0 end
    end
    local isok2 = false
    local banker_rules = Banker_FillCard_Rules[banker_points]
    if ((player_points > 7 or banker_points > 7) and not spin_result.banker_cards[3])
    or (banker_rules and not banker_rules[key] and spin_result.banker_cards[3])
    or ((not banker_rules or banker_rules[key]) and not spin_result.banker_cards[3]) then 
        isok2 = true
    end
    if not isok2 then
        filelog.sys_error("-----------banker fill card rule error ------",spin_result)
        return false
    end

    --判断点数
    local is_fillcard = false
    if spin_result.banker_cards[3] then
        local cv = spin_result.banker_cards[3] % 100
        if cv < 10 then
            banker_points = (banker_points + cv) % 10
        end
        is_fillcard = true
    end
    if spin_result.player_cards[3] then
        local cv = spin_result.player_cards[3] % 100
        if cv < 10 then
            player_points = (player_points + cv) % 10
        end
        is_fillcard = true
    end
    if (banker_points ~= spin_result.banker_points)
    or (player_points ~= spin_result.player_points) then
        filelog.sys_error("-----------banker or player points not is equal ------",spin_result)
        return false
    end

    --判断补牌标记
    if (is_fillcard and not spin_result.is_fillcard)
    or (not is_fillcard and spin_result.is_fillcard) then
        filelog.sys_error("-----------fill card flag is error------",spin_result)
        return false
    end

    --判断最终结果
    local result_id = EBJLBetBtnId.BET_BTN_ID_BANKER
    if player_points > banker_points then
        result_id = EBJLBetBtnId.BET_BTN_ID_PLAYER 
    elseif player_points == banker_points then
        result_id = EBJLBetBtnId.BET_BTN_ID_TIE
    end
    if spin_result.prize_id ~= result_id then
        filelog.sys_error("-----------prize_id is error------",spin_result)
        return false
    end

    return true
end

--
--处理送灯
--is_seq是否为连续灯
--
function SlotTool.handle_give_lamp(sd_w_list, is_seq)
    local result_list = {}

    --获得送灯数
    local lamp_num = SlotTool.get_random_result_by_weightlist(sd_w_list)
    if not lamp_num then
        return
    end

    --计算总权值
    local total_w = SlotTool.calculate_total_weight(SDFruit_weight_list)

    --随机获取lamp_num盏灯
    if is_seq then  --连续lamp_num盏灯
        local id = SlotTool.get_random_result_by_weightlist(SDFruit_weight_list,total_w)
        table.insert(result_list,id)
        table.insert(result_list,lamp_num)
    else
        for i = 1,lamp_num do
            local id = SlotTool.get_random_result_by_weightlist(SDFruit_weight_list,total_w)
            table.insert(result_list,id)
        end
    end
    
    return result_list
end

--
--处理闪电
--
function SlotTool.handle_lightning(lgtn_w_list)
    local result_list = {}

    --获得翻倍数
    local rate_num = SlotTool.get_random_result_by_weightlist(lgtn_w_list)
    if not rate_num then
        return
    end
    table.insert(result_list,rate_num)

    return result_list
end

--
--根据随机结果获取额外奖励项
--
function SlotTool.get_ext_prize_item(prize_id)
    
end

--
--保存奖励记录
--
function SlotTool.save_prize_records(gamename, opencard_result)
    local records = {}
    local key = (gamename or "") .. "_prize_records"
    local result,data = redisdao.query_data(".gmmqredis", "get", key)
    if result and data then
        records = json.decode(data) or {}
    end
    
    for _,prize_id in pairs(opencard_result) do
        local id = tostring(prize_id)
        records[id] = (tonumber(records[id]) or 0) + 1
    end
    records.total_count = (tonumber(records.total_count) or 0) + 1
    redisdao.query_data(".gmmqredis", "set", key, json.encode(records))

    ---------------------------------

    --辅助函数1
    local function helper_func(val)
        local cnt = 0
        local tmp = val
        while val >= 10 do
            val = math.floor(val / 10)
            cnt = cnt + 1
        end
        if math.floor(10^cnt) == tmp then
            return true
        end
        return false
    end

    --辅助函数2
    local function func(records)
        local porp_list = {}
        local subprop_list = {}
        local cnt = records.total_count or 0
        if cnt == 0 then
            return
        end

        for k,v in pairs(records) do
            if type(v) ~= "table" and k ~= "total_count" then
                v = tonumber(v) or 0
                porp_list[k] = string.format("%g%%",math.floor(v / cnt * 10000) / 100)
            elseif k == "total_count" then
                porp_list[k] = v
            else
                subprop_list[k] = func(v)
            end
        end
        return porp_list,subprop_list
    end

    --开奖次数每增加10倍，统计一次概率
    if helper_func(records.total_count) then
        local porp_list,subprop_list = func(records)
        filelog.sys_obj("prop_list", records.total_count, porp_list, "=================\n", subprop_list)
    end
    ---------------

    return true
end

--
--创建格式化赔率列表
--
function SlotTool.create_format_odds_list()
    local fmt_odds_list = {}

    for id,odds in pairs(Odds_list) do
        local val = tonumber(id) * 10000 + tonumber(odds)
        table.insert(fmt_odds_list,val)
    end
    
    return fmt_odds_list
end

--
--保存赔率列表到文件
--
function SlotTool.save_odds_list_to_file()
    local f = io.open("odds_list.lua","w+")
    if not f then
        filelog.print("open file error!")
        return
    end

    f:write("return { \n")
    for id,odds in pairs(Odds_list) do
        local val = tonumber(id) * 10000 + tonumber(odds)
        f:write(tostring(val) .. ",")
    end
    f:write("}")
    f:close()
end


-------------------------------
----------
--外部接口

local CMD = {}

local cs = queue()

-----------------
----

--
--获取随机旋转结果
--total_bet_list 总下注列表
--total_bet 总下注值
--avg_bet 平均下注值
--is_robot_banker 是否机器人做庄
--is_random 是否随机开奖
--
function CMD.get_random_spin_result(gamename, total_bet_list, total_bet, avg_bet, is_robot_banker, is_random)
    local cnt = 0
    while cnt < 100 do
        local isok,spin_result = skynet.pcall(SlotTool.gen_random_spin_result,gamename,total_bet_list,total_bet,avg_bet,is_robot_banker, is_random)
        if isok and SlotTool.check_random_spin_result(spin_result) then
            return spin_result
        end
        cnt = cnt + 1
    end

    return false
end

--
--根据随机结果获取额外奖励
--
function CMD.get_ext_prize_result(prize_id)
    local ext_prize_result = {}

    --根据id获取奖励项
    local item = SlotTool.get_ext_prize_item(prize_id)
    if not item then
        return
    end

    --计算总权值
    local total_w = 0
    local w_list = {}
    for key,val in pairs(item) do
        if type(key) == "string" and string.sub(key,-1) == "w" then
            total_w = total_w + val
            w_list[key] = val
        end
    end
    
    --根据权值表，获取随机结果
    local key = SlotTool.get_random_result_by_weightlist(w_list,total_w)
    if not key or key == "failed_w" then
        return
    end

    --获取额外奖励类型
    local len = string.len(key)
    local extprize_name = string.sub(key,1,len - 2)
    local extprize_type = Extra_prize_type[extprize_name]
    if not extprize_type then
        return
    end

    --获取奖励
    local extprize_list = nil
    
    --返回结果
    ext_prize_result.extprize_type = extprize_type
    ext_prize_result.extprize_list = extprize_list

    return ext_prize_result
end

--
--获取开局奖励
--
function CMD.get_start_prize_result(rids)
    local prize_result_list = {}

    if true then
        return {}
    end

    --获取随机结果
    local function get_random_result(prize_list)
        --计算总权值
        local total_w = 0
        local w_list = {}
        for key,val in pairs(prize_list) do
            if type(key) == "string" and string.sub(key,-1) == "w" then
                total_w = total_w + val
                w_list[key] = val
            end
        end
    
        --根据权值表，获取随机结果
        local key = SlotTool.get_random_result_by_weightlist(w_list,total_w)

        return key
    end

    -------------------

     --开火车奖励表
    local drivetrain_list = SPrize_weight_list[SpecialPrize.SPECIAL_PRIZE_KHC]

    --根据权值表，获取随机结果
    local key = get_random_result(drivetrain_list)
    if not key or key == "failed_w" then
        
    else
        local extprize_list = SlotTool.handle_give_lamp(drivetrain_list.sd_w_list,true)
        local item = {
            extprize_type = Extra_prize_type.drivetrain,
            extprize_list = extprize_list,
        }
        table.insert(prize_result_list,item)
    end

    ------------------

    rids = rids or {}
    
    --低于10人不触发
    local player_num = #rids
    if player_num < 10 then
        return prize_result_list
    end

     --判断是否触发鸿运当头
    local goodluck_list = SPrize_weight_list[SpecialPrize.SPECIAL_PRIZE_HYDT]
    local key = get_random_result(goodluck_list)
    if not key or key == "failed_w" then
        return prize_result_list
    end

    --随机选取10%的人中奖
    local prize_coin_list = {}
    local total_w = SlotTool.calculate_total_weight(goodluck_list.coin_w_list)
    local hit_num = math.floor(player_num * 0.1)
    for i = 1,hit_num do
        local k = base.get_random(1,player_num)
        local rid = rids[k]
        local coin_num = SlotTool.get_random_result_by_weightlist(goodluck_list.coin_w_list,total_w)

        prize_coin_list[rid] = coin_num
        rids[k] = rids[player_num]
        table.remove(rids)
        player_num = #rids
    end
    
    if hit_num > 0 then
        local item = {
            extprize_type = Extra_prize_type.goodluck,
            extprize_list = prize_coin_list,
        }
        table.insert(prize_result_list,item)
    end
 
    return prize_result_list
end

--
--处理开奖
--
function CMD.handle_open_jackpot(gamename, jackpot_id, player_list)
    local award_list = {}

    --获取奖池金额
    local jackpot_money = SlotTool.get_jackpot_from_redis(gamename,jackpot_id)

    --随机开奖
    local total_coin = 0
    local max_level = 6
    for _,item in pairs(player_list) do
        local level = math.floor(item.game_num / 5)
        if level > max_level then
            level = max_level
        end
        local w_list = Jackpot_weight_list[level]
        local award_id = SlotTool.get_random_result_by_weightlist(w_list)
        local award_coin = math.floor(Jackpot_award_list[award_id] * jackpot_money * 0.01)
        local award_item = {
            rid = item.rid,
            logo = item.logo,
            rolename = item.rolename,
            award_money_num = award_coin,
        }
        table.insert(award_list, award_item)
        total_coin = total_coin + award_coin
    end
    
    return award_list,total_coin
end

--
--保存奖励记录
--
function CMD.save_prize_records(gamename, opencard_result)
  return cs(SlotTool.save_prize_records,gamename,opencard_result)
end

--
--处理庄家收益
--
function CMD.handle_banker_profit(ganmename, total_bet_num, player_win_num)
    local banker_win = total_bet_num - player_win_num
    return banker_win
end

--
--更新图标权值表
--
function CMD.update_picture_weight_list(new_w_list)
    if type(new_w_list) ~= "table" then
        return false
    end

    for k,v in pairs(new_w_list) do
        if Weight_list[k] and type(v) == "table" then
            for id,w in pairs(v) do
                if Weight_list[k][id] and w >= 0 then
                    Weight_list[k][id] = w
                end
            end
        end
    end
    
    --更新化概率表,总权值
    if next(new_w_list) ~= nil then
        Prob_list,Total_weight_list = SlotTool.update_picture_prob_list()
    end

    return true
end

--
--设置系统输钱上限
--
function CMD.set_system_lose_limit(loselimit_base, loselimit_factor)
    loselimit_base = loselimit_base or 0
    loselimit_factor = loselimit_factor or 0.02
    if loselimit_base < 0 or loselimit_factor < 0 then
        return false
    end
    SystemLoseLimit_Base = loselimit_base
    SystemLoseLimit_Factor = loselimit_factor

    return true
end

--
--处理结算结果
--@total_change 总变化值
--@total_water  总流水值
--@total_bet    玩家总下注
--
function CMD.handle_balance_result(gamename, total_change, total_water, total_bet, comment)
    local tax_rate = SlotTool.get_tax_rate_from_redis(gamename)
    local profit_val = math.floor(total_bet * tax_rate * 100) / 100
    local inventory_val = -total_change

    --更新收益值
    local result1 = SlotTool.update_system_profit(gamename, profit_val, comment)
    if result1 then
        inventory_val = inventory_val - profit_val
    end

    --更新库存值，扣除抽水
    local service_fee_rate = SlotTool.get_service_fee_rate_from_redis(gamename)
    local service_fee_val = math.floor(total_water * service_fee_rate * 100) / 100
    inventory_val = inventory_val - service_fee_val

    return SlotTool.update_system_inventory(gamename, inventory_val, comment)
end

--
--获取抽水率
--
function CMD.get_service_fee_rate(gamename)
    return SlotTool.get_service_fee_rate_from_redis(gamename)
end

-----------
--
--初始化
--
local function init()

    --初始化概率表,总权值
    Prob_list,Total_weight = SlotTool.update_picture_prob_list()

    --保存赔率列表到文件
    SlotTool.save_odds_list_to_file()

    --filelog.print("init slot tool ok!")
end

-------------------
--
skynet.start(function()
    init()
    skynet.dispatch("lua",function(session, source, cmd, ...)
        local f = CMD[cmd]
        if f then
            skynet.retpack(f(...))
        else
            skynet.retpack()
        end
    end)
end)